package br.com.trabalho4.controller;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.JMenuItem;
import javax.swing.JSlider;
import javax.swing.JTextField;

import br.com.trabalho4.model.shape.ConvexShape;
import br.com.trabalho4.model.shape.Line;
import br.com.trabalho4.model.shape.Point;
import br.com.trabalho4.model.shape.Shape;
import br.com.trabalho4.view.MainWindow;

public class Controller {
	
	private MainWindow mainWindow;
	
	private JFileChooser fileChooser;
	
	private JMenuItem menuAbrir;
	private JTextField textFieldZoom;
	private JSlider sliderZoom;
	
	public Controller(MainWindow mainWindow) {
		this.mainWindow = mainWindow;
		
		fileChooser = new JFileChooser();
		
		this.menuAbrir = mainWindow.getMenuAbrir();
		this.textFieldZoom = mainWindow.getTextFieldZoom();
		this.sliderZoom = mainWindow.getSliderZoom();
		
		initialize();
	}
	
	private void initialize() {
		menuAbrir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				
				int returnVal = fileChooser.showOpenDialog(mainWindow.getFrameTrabalho());
				
				if (returnVal == JFileChooser.APPROVE_OPTION) {
		            File file = fileChooser.getSelectedFile();
		            
		            //This is where a real application would open the file.
		            System.out.println("Opening: " + file.getName());
		            
		            //mainWindow.setFileTextura(file);
		            readInputFile(file);
		        } else {
		        	//checkBoxAtivarTextura.setSelected(false);
		        	System.out.println("Open command cancelled by user.");
		        }
			}
		});
		
		sliderZoom.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent event) {
				textFieldZoom.setText(sliderZoom.getValue()*50+"%");
				
				mainWindow.getJPaintPanel().setZoom(sliderZoom.getValue());
				mainWindow.getJPaintPanel().repaint();
			}
		});
	}
	
	private void readInputFile(File f) {
		System.out.println("Reading: " + f.getName());
		
		try {
			mainWindow.getJPaintPanel().removeAllShapes();
			
			FileReader fileReader = new FileReader(f);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			
			String linha = bufferedReader.readLine();
			//int qtdLinhas = Integer.parseInt(linha);
			
			List<Point> pointList = new ArrayList<Point>();
			
			while (linha != null) {
				while(linha != null && linha.startsWith("#")){
					linha = bufferedReader.readLine();
				}
				
				if (linha == null) {
					break;
				}
				System.out.println(linha);
				
				linha = linha.replaceAll("\\[", "");
				linha = linha.replaceAll("\\]", "");
				linha = linha.replaceAll("\\(", "");
				linha = linha.replaceAll("\\)", "");
				linha = linha.replaceAll(" ", "");
				
				//System.out.println(linha);
				
				String objetos[] = linha.split(";");
				
				for (String o : objetos) {
					//System.out.println(o);
					
					String parametros[] = o.split(",");
					
					int x = Integer.parseInt(parametros[0]);
					int y = Integer.parseInt(parametros[1]);
					int r = Integer.parseInt(parametros[2]);
					int g = Integer.parseInt(parametros[3]);
					int b = Integer.parseInt(parametros[4]);
					
					pointList.add(new Point(x, y, new Color(r, g, b)));
				}
				
				Shape s;
				
				if (pointList.size() == 1) {
					s = (Point)pointList.get(0);
				} else if (pointList.size() == 2) {
					s = new Line(pointList.get(0), pointList.get(1));
				} else {
					s = new ConvexShape(pointList);
				}
				
				addShape(s);
				
				pointList.clear();
				
				linha = bufferedReader.readLine();
			}
			
			mainWindow.getJPaintPanel().repaint();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void addShape(Shape s) {
		mainWindow.getJPaintPanel().addShape(s);
	}
	
	public void removeShape(Shape s) {
		mainWindow.getJPaintPanel().removeShape(s);
	}
}
